﻿@{
    ViewBag.Title = "Handvn";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@*@model handvn.Models.Product*@
@*@model IEnumerable<handvn.Models.Product>*@

@model handvn.Models.HomeView
<div class="tl_content">
    <div class="tl_title_product_view">
        Sản phẩm theo chi tiết danh mục</div>
    <div id="gridcontainer">
        
        @foreach (var item in ViewBag.OnePageOfProducts)
        {  
        <div class="tl_item">
            <a href="../product/viewdetail?prod=@item.ProductID">
                <img class="tl_item_img" src="../../Images/HinhAnh/@item.HinhAnh" alt=""/></a>
            <a  class="tl_item_title" href="../product/viewdetail?prod=@item.ProductID">@item.TenSanPham</a>
            @* @Html.ActionLink(item.TenSanPham, "../DetailProduct/View", new { ProductID = item.ProductID })*@
            <a  class="tl_item_user" href="../User/View?user=@item.UserID">@item.User.TenKhachhang</a>
            <div class="tl_item_rice">@item.GiaSanPham vnd</div>
        </div>        
        }
        <br />  
        @*
************************
***** HIEN SO TRANG ****
************************
*@

@helper buildLinks(int start, int end, string innerContent)
{  
    for (int i = start; i <= end; i++)
    {
        <a class="@(i == ViewBag.CurrentPage ? "tl_current" : "")" href="/product/view?decaid=@ViewBag.did&page=@i" >@(innerContent ?? i.ToString())</a> 
    } 
}
@helper pageLinks()
{
    //const int maxPages = 11;
    const int maxPages = 6;
    int pre = ViewBag.CurrentPage - 1;
    int next = ViewBag.CurrentPage + 1;
    if (pre > 0)
    {
        <a href="/product/view?decaid=@ViewBag.did&page=@pre" > << </a> 
    }

    if (ViewBag.TotalPages <= maxPages)
    {
        @buildLinks(1, (int)ViewBag.TotalPages, null)
        if (next <= ViewBag.TotalPages)
        {
            <a href="/product/view?decaid=@ViewBag.did&page=@next" >>> </a> 
        }
        return;
    }

    int pagesAfter = ViewBag.TotalPages - ViewBag.CurrentPage; // Number of pages after current
    int pagesBefore = ViewBag.CurrentPage - 1; // Number of pages before current

    if (pagesAfter <= 4)
    {
        @buildLinks(1, 1, null) // Show 1st page

        int pageSubset = ViewBag.TotalPages - maxPages - 1 > 1 ? ViewBag.TotalPages - maxPages - 1 : 2;
        @buildLinks(pageSubset, pageSubset, "...") // Show page subset (...)

        @buildLinks(ViewBag.TotalPages - maxPages + 3, ViewBag.TotalPages, null) // Show last pages
        if (next <= ViewBag.TotalPages)
        {
            <a href="/product/view?decaid=@ViewBag.did&page=@next" >>> </a> 
        }
        return; // Exit
    }
        
    if (pagesBefore <= 4)
    {
        @buildLinks(1, maxPages - 2, null) // Show 1st pages

        int pageSubset = maxPages + 2 < ViewBag.TotalPages ? maxPages + 2 : ViewBag.TotalPages - 1;
        @buildLinks(pageSubset, pageSubset, "...") // Show page subset (...)

        @buildLinks(ViewBag.TotalPages, ViewBag.TotalPages, null) // Show last page
                                                                      
        if (next <= ViewBag.TotalPages)
        {
            <a href="/product/view?decaid=@ViewBag.did&page=@next" >>> </a> 
        }
        return; // Exit
    }

    if (pagesAfter > 4)
    {
        @buildLinks(1, 1, null) // Show 1st pages

        int pageSubset1 = ViewBag.CurrentPage - 7 > 1 ? ViewBag.CurrentPage - 7 : 2;
        int pageSubset2 = ViewBag.CurrentPage + 7 < ViewBag.TotalPages ? ViewBag.CurrentPage + 7 : ViewBag.TotalPages - 1;

        @buildLinks(pageSubset1, pageSubset1, pageSubset1 == ViewBag.CurrentPage - 4 ? null : "...") // Show 1st page subset (...)

        @buildLinks(ViewBag.CurrentPage - 3, ViewBag.CurrentPage + 3, null) // Show middle pages

        // Show 2nd page subset (...)
        // only show ... if page is contigous to the previous one.
        @buildLinks(pageSubset2, pageSubset2, pageSubset2 == ViewBag.CurrentPage + 4 ? null : "...")
        @buildLinks(ViewBag.TotalPages, ViewBag.TotalPages, null) // Show last page

        if (next <= ViewBag.TotalPages)
        {
            <a href="/product/view?decaid=@ViewBag.did&page=@next" >>> </a> 
        }
        return; // Exit

    }              
}
<div class="tl_pagination">
    @pageLinks()
</div>


    </div>
</div>
<!-- End Content-->
